package me.xue.communityhealth.admin.controller;

import lombok.extern.log4j.Log4j2;
import me.xue.communityhealth.data.ResponseEntity;
import me.xue.communityhealth.exception.IllegalRequestException;
import me.xue.communityhealth.model.Registration;
import me.xue.communityhealth.service.RegistrationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@Controller
@Log4j2
public class RegistrationController {

    private final RegistrationService registrationService;

    @Autowired
    public RegistrationController(RegistrationService registrationService) {
        this.registrationService = registrationService;
    }

    @GetMapping("/registrations")
    public String registrations(Model model) {
        List<Registration> registrations = registrationService.findAll();
        model.addAttribute("registrations", registrations);
        return "registrations";
    }

    @PostMapping("/registration")
    @ResponseBody
    public ResponseEntity addRegistration(@Valid Registration registration, BindingResult bindingResult) {
        ResponseEntity responseEntity = new ResponseEntity();
        responseEntity.result = "fail";
        if (bindingResult.hasErrors()) {
            responseEntity.message = "添加失败，请检查字段";
            return responseEntity;
        }
        int saved = registrationService.save(registration);
        if (saved == 1) {
            responseEntity.result = "success";
//            Registration registration1 = registrationService.findRegistrationByName(registration.getName());
//            responseEntity.message = registration1.getId().toString();
            return responseEntity;
        }
        responseEntity.message = "添加失败";
        return responseEntity;
    }

    @PutMapping("/registration")
    @ResponseBody
    public ResponseEntity updateRegistration(@Valid Registration registration, BindingResult bindingResult) {
        ResponseEntity responseEntity = new ResponseEntity();
        responseEntity.result = "fail";
        if (bindingResult.hasErrors()) {
            responseEntity.message = "修改失败，请检查字段";
            return responseEntity;
        }
        int saved = registrationService.update(registration);
        if (saved == 1) {
            responseEntity.result = "success";
            return responseEntity;
        }
        responseEntity.message = "修改失败";
        return responseEntity;
    }

    @DeleteMapping("/registration/{id:\\d+}")
    @ResponseBody
    public String deleteRegistration(@PathVariable("id") Integer id) {
        if (id < 0) {
            throw new IllegalRequestException();
        }
        int deleted = registrationService.delete(id);
        if (deleted == 1) {
            return "success";
        }
        return "删除失败，请重试";
    }

}
